package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class YXGAttr implements AdapterAttr {


    public static void main(String[] args) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", "FC72EEEF865B05F63106FC8C14D768D5");
        params1.put("merchant_no", "youyouwangyou");
        params1.put("gameid", 22);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm("https://api.taohaoge.com/interfaces/getExpendAttribute.ashx", params1, head);
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String result = "[{\"id\":\"1\",\"name\":\"穿越火线\"},{\"id\":\"2\",\"name\":\"枪战王者\"},{\"id\":\"3\",\"name\":\"王者荣耀\"},{\"id\":\"4\",\"name\":\"火影忍者\"},{\"id\":\"5\",\"name\":\"和平精英\"},{\"id\":\"6\",\"name\":\"原神\"},{\"id\":\"7\",\"name\":\"飞车手游\"},{\"id\":\"8\",\"name\":\"飞车端游\"},{\"id\":\"9\",\"name\":\"英雄联盟\"},{\"id\":\"10\",\"name\":\"逆战\"},{\"id\":\"11\",\"name\":\"永劫无间\"},{\"id\":\"12\",\"name\":\"地下城与勇士\"},{\"id\":\"13\",\"name\":\"光遇\"},{\"id\":\"14\",\"name\":\"DNF手游\"},{\"id\":\"15\",\"name\":\"蛋仔派对\"},{\"id\":\"16\",\"name\":\"金铲铲之战\"},{\"id\":\"17\",\"name\":\"球球大作战\"},{\"id\":\"18\",\"name\":\"崩坏-星穹铁道\"},{\"id\":\"19\",\"name\":\"无畏契约\"},{\"id\":\"20\",\"name\":\"逆水寒\"},{\"id\":\"21\",\"name\":\"使命召唤\"},{\"id\":\"22\",\"name\":\"绝地求生\"},{\"id\":\"23\",\"name\":\"第五人格\"}]\n";
        List<JSONObject> jsonObjects = JSONObject.parseArray(result, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", salePartner.getSaleKey());
        params1.put("merchant_no", salePartner.getOther());
        params1.put("gameid", gameId);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm("http://api.youxige.com/tools/getAreaServer.ashx", params1, head);
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("arealist"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setId(datum.getString("id"));
            interServer.setName(datum.getString("name"));
            String servers = datum.getString("serverlist");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.getString("id");
                    String title1 = object.getString("name");
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params1 = new TreeMap<>();
        Map<String, Object> head = new HashMap<>();
        head.put("content-type", "application/x-www-form-urlencoded");
        params1.put("key", salePartner.getSaleKey());
        params1.put("merchant_no", salePartner.getOther());
        params1.put("gameid", gameId);
        params1.put("content-type", "application/x-www-form-urlencoded");
        String response = HttpUtils.doPostForm("http://api.youxige.com/tools/getExpendAttribute.ashx", params1, head);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("fieldtype").equals("multi-radio")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("fieldtype").equals("multi-checkbox")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("fieldtype").equals("multi-input")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            String string = object.getString("options");
            JSONArray jsonArray = JSONArray.parseArray(string);
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(object.getString("fieldname"));
            interAttrRes.setName(object.getString("propertyname"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            for(Object o : jsonArray) {
                InterAttrRes interAttrRes1 = new InterAttrRes();
                interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("key"));
                interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("value"));
                interAttrResList1.add(interAttrRes1);
                interAttrRes.setInterServerRes(interAttrResList1);
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}